package com.aimaoblog.admin.service;

import com.aimaoblog.admin.api.TagAdminService;
import com.aimaoblog.admin.dao.TagMapper;
import com.aimaoblog.constant.ServerResponse;
import com.aimaoblog.pojo.Tag;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * @Author PuaChen
 * @Create 2018-07-03 17:33
 */
@Transactional(rollbackFor = Exception.class)
@Service
public class TagAdminServiceImpl implements TagAdminService {

    @Autowired
    private TagMapper tagMapper;

    @Override
    public ServerResponse<PageInfo<Tag>> findAll(boolean isSplit, Integer page, Integer size) {
        if (isSplit) {
            if (page == null || size == null) {
                return ServerResponse.createFailure("参数错误");
            }
            PageHelper.startPage(page, size);
        }
        return ServerResponse.createSuccessAndData(new PageInfo<>(tagMapper.selectAll()));
    }

    @Override
    public ServerResponse<PageInfo<Tag>> findToLike(String keyword, boolean isSplit, Integer page, Integer size) {
        if (StringUtils.isBlank(keyword)) {
            return ServerResponse.createFailure("关键字不能为空");
        }
        if (isSplit) {
            if (page == null || size == null) {
                return ServerResponse.createFailure("参数错误");
            }
            PageHelper.startPage(page, size);
        }
        keyword = "%" + keyword + "%";
        return ServerResponse.createSuccessAndData(new PageInfo<>(tagMapper.selectAllByKeyWord(keyword)));
    }

    @Override
    public ServerResponse<Tag> addTag(Tag tag) {
        if (tag == null) {
            return ServerResponse.createFailure("参数错误");
        }
        tag.setId(null);
        tag.setCreatetime(new Date());
        int i = tagMapper.insertSelective(tag);
        if (i == 0) {
            return ServerResponse.createFailure("插入失败");
        }
        return ServerResponse.createSuccess();
    }

    @Override
    public ServerResponse<Tag> updateTag(Integer id, String name) {
        if (id == null || name == null) {
            return ServerResponse.createFailure("参数错误");
        }
        Tag tag = new Tag();
        tag.setId(id);
        tag.setName(name);
        int i = tagMapper.updateByPrimaryKeySelective(tag);
        if (i == 0) {
            return ServerResponse.createFailure("更新失败");
        }
        return ServerResponse.createSuccess();
    }

    @Override
    public ServerResponse deleteTag(Integer id) {
        if(id==null){
            return ServerResponse.createFailure("参数错误");
        }
        int row = tagMapper.deleteByPrimaryKey(id);
        if (row <= 0) {
            return ServerResponse.createFailure("删除失败");
        }
        return ServerResponse.createSuccess();
    }
}
